package array;

import java.util.Arrays;

/**
 * @author 孤雪唯方
 * @date 2025/05/10
 * @description 数组
 */
public class Array {

    /**
     * 冒泡排序(升序)
     *
     * @param array 数组
     */
    public static void bubbleSort(int[] array){
        for (int i = 0 ;i < array.length;i++){
            for(int j = 1;j<array.length-i;j++){
                if(array[j-1]>array[j]){
                    int temp = array[j-1];
                    array[j-1] = array[j];
                    array[j] = temp;
                }
            }
        }
    }

    public static void main(String[] args) {
        //数组定义及初始化
        int[] array = new int[10];
        double[] array1 = new double[]{1.2, 3.4, 5.6};
        String[] array2 = {"apple", "banana", "cherry"};
        array[0] = 10;
        array[1] = 20;
        array[2] = 30;

        //打印数组长度及元素
        System.out.println(array.length);
        System.out.println(array1.length);
        System.out.println(array2.length);
        System.out.println("以上是数组长度");

        for (int i = 0; i < array.length; i++){
            System.out.println(array[i]);
        }

        System.out.println("以上为int[]数组元素");
        for (double v : array1) {
            System.out.println(v);
        }
        System.out.println("以上为double[]数组元素");

        for(String s: array2){
            System.out.println(s);
        }
        System.out.println("以上为String[]数组元素");

        //对数组array进行随机赋值
        for (int i = 0; i < array.length; i++){
            array[i] = (int)(Math.random()*100);
            System.out.println(array[i]);
        }
        //对array数组转字符串
        String str = Arrays.toString(array);
        System.out.println(str);

        //冒泡排序
        bubbleSort(array);
        System.out.println("冒泡排序后：");
        for(int i: array){
            System.out.println(i);
        }
    }
}
